 
#include "headers/user_progs.h"
#include "../api/headers/io.h"
#include "../multitasking/headers/task.h"
#include "../memory/headers/kheap.h"
#include "../api/headers/string.h"
#include "../api/headers/errors.h"
extern u32int timer_ticks;
extern task_t* current_task;
void tick()
{
	u32int start = timer_ticks;
	while(timer_ticks < start + 1000)
	{
		printf("%d  ", timer_ticks);
		block_process(current_task, RES_TIME, timer_ticks + 500);
	}
	int a = 0;
	a  = 5 / a;

}

void lameWhile()
{
	u32int i = 0;
	for(i = 0; ;i++)
	{
		setTextColour(i%15);
		printf("%c%C%d %d");
		block_process(current_task, RES_TIME, timer_ticks + 10);
	}
}

void secondProg()
{
	printf("Hello\n");
}
void* ptr[111];
s8int buff[255];

void memTest()
{
	
	int i = 1, a= 0, lim = 0, allc=0;
	printf("Starting memory test.\n");
	
	for(i = 1;;i++)
	{
		lim = timer_ticks % 100 + 10;
		for(a = 1; a < lim; a++)
		{
			if(i % 2)
			{
				if(!ptr[a-1])
				{
					printf("Allocattting %d bytes.\n", a*a*10);
					ptr[a-1] = kmalloc(a*a*10);
					if(getLastError() != NO_ERROR)
					{
						printf("Error allocating: %s\n", getLastErrorMessage());
						getString((s8int*)buff);
					}
				}
				
			}
			else
			{
				if(ptr[a-1])
				{
					printf("Deallocating: 0x%H\n", ptr[a-1]);
					kfree(ptr[a-1]);
					if(getLastError() != NO_ERROR)
					{
						printf("Error freeing: %s\n", getLastErrorMessage());
						getString((s8int*)buff);
					}
					ptr[a-1] = 0;
				}
				//getString((s8int*)buff);
			}
			
		}
		
		getString((s8int*)buff);

	}
}

void killHeap()
{
	printf("Self destruct sequence initiated.\nSelf desstructing in\n");
	int a = 4;
	
	while(a--)
	{
		printf("%d\n", a);
		block_process(current_task, RES_TIME, timer_ticks + 100);
	}
	printf("0");
	for(;;)
	{
		kmalloc(1000);
		if(getLastError() != NO_ERROR)
		{
			printf("Error: %s\n", getLastErrorMessage());
			getString((s8int*)buff);
		}
	}
}